<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>新增图书</title>
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/lib/layui-v2.6.3/css/layui.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/css/layuimini.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/css/themes/default.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/lib/font-awesome-4.7.0/css/font-awesome.min.css') }}" media="all">
    <style>
        body{
            padding: 10px;
        }
        .layui-form-select {
            width: 50%; /* 设置下拉框的宽度为50% */
        }
        .layui-textarea{
            min-height: 200px;
        }
    </style>
</head>
<body style="background-color: white">
<form class="layui-form" action="">
    <div style="display: flex">
        <div class="layui-form-item" style="width: 200px">
            <label class="layui-form-label">图书编号</label>
            <div class="layui-input-block">
                <input type="text" name="bookid" required  lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" >
            <label class="layui-form-label">图书名字</label>
            <div class="layui-input-inline" style="margin-right: 0">
                <input type="text" id="bookname" name="bookname" required lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图书类型</label>
        <div class="layui-input-block">
            <select name="booksort" lay-verify="required">
                <option value="">请选择：</option>
                <option value="文学">文学</option>
                <option value="科幻经典">科幻经典</option>
                <option value="历史">历史</option>
                <option value="专业书">专业书</option>
            </select>
        </div>
    </div>
    <div style="display: flex">
        <div class="layui-form-item" style="width: 250px">
            <label class="layui-form-label">图书作者</label>
            <div class="layui-input-block">
                <input type="text" name="bookauthor" required  lay-verify="required"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="width: 200px">
            <label class="layui-form-label">图书价格</label>
            <div class="layui-input-block">
                <input type="text" name="bookprice" required  lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">图书简介</label>
        <div class="layui-input-block">
            <textarea style="width: 50%" name="bookdinfo" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
    </div>
    <div style="margin-bottom: 30px">
        <div class="layui-upload" style="display: flex">
            <button type="button" class="layui-btn" id="test1" style="margin-left: 40px">上传图片</button>
            <div style="margin-left: 20px">
                <div class="layui-upload-list" style="margin:0">
                    <img class="layui-upload-img" id="demo1" style="width: 92px;height: 92px">
                    <p id="demoText"></p>
                </div>
                <div style="width: 95px;margin-top: 5px">
                    <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="demo">
                        <div class="layui-progress-bar" lay-percent=""></div>
                    </div>
                </div>
            </div>
            <input type="hidden" id="pngname" name="pngname">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button id="btn-submit" class="layui-btn" lay-submit lay-filter="btn-submit-filter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="{{ url_for('static',filename='layuimini/lib/layui-v2.6.3/layui.js') }}" charset="utf-8"></script>
<script>
    layui.use(['laydate','table','form','upload', 'element', 'layer'],function () {
        var laydate=layui.laydate;
        var table=layui.table;
        var form=layui.form;
        var $=layui.$;
        var upload = layui.upload
            ,element = layui.element
            ,layer = layui.layer;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '/admin/uploadImg'
            ,method:'POST'
            ,before: function(obj){
                console.log(document.getElementById("bookname").value)
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
                element.progress('demo', '0%'); //进度条复位
                layer.msg('上传中', {icon: 16, time: 0});
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                console.log(res)
                document.getElementById("pngname").value=res.filename;
                //上传成功的一些操作
                $('#demoText').html(''); //置空上传失败的状态
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
            //进度条
            ,progress: function(n, elem, e){
                element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
                if(n == 100){
                    layer.msg('上传完毕', {icon: 1,time:1000});
                }
            }
        });

        //表单提交
        form.on('submit(btn-submit-filter)', function(data){
            console.log($('input[name="bookname"]').val())
            $("#btn-submit").attr("disabled","disabled").addClass("layui-btn-disabled");
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            //异步请求提交数据
            $.ajax({
                url:"/admin/addbook",
                data:data.field,
                success:function (response) {
                    if(response.state==200){
                        //成功提示
                        layer.msg(response.message, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            //刷新表格
                            parent.refreshTable();
                            //关闭窗口
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                        });
                    }else {
                        $("#btn-submit").removeAttr("disabled","disabled").removeClass("layui-btn-disabled");
                        layer.msg("新增失败！", {
                            icon: 2,
                            anim: 6
                        });
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
</body>
</html>